package com.mlizhi.modules.spec.setting;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.mlizhi.base.MlzApplication;
import com.mlizhi.base.Session;
import com.mlizhi.base.VersionManager;
import com.mlizhi.kingdom.R;
import com.mlizhi.modules.login.LoginActivity;
import com.mlizhi.modules.spec.dao.ContentDao;
import com.mlizhi.modules.spec.dao.DaoSession;
import com.mlizhi.modules.spec.dao.DetectDao;
import com.umeng.analytics.AnalyticsConfig;
import com.umeng.analytics.MobclickAgent;

public class SpecSettingListActivity extends Activity {
	//

	private MlzApplication mlzApplication;
	private Session mSession;
	private DaoSession daoSession;
	private DetectDao detectDao;
	private ContentDao contentDao;
	private ProgressDialog cacheProgressDialog;
//	private RequestQueue mRequestQueue;
	private Context mContext;
	private Activity currrentActivity;
	private TextView versionTextView;
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		currrentActivity = this;
		mlzApplication = (MlzApplication) getApplication();
		mSession = Session.get(getApplicationContext());
		this.setContentView(R.layout.activity_spec_setting_list);
		/** 发送策略定义了用户由统计分析SDK产生的数据发送回友盟服务器的频率。 */
		MobclickAgent.updateOnlineConfig( mContext );
		/** 设置是否对日志信息进行加密, 默认false(不加密). */
		AnalyticsConfig.enableEncrypt(true);
		versionTextView = (TextView) findViewById(R.id.id_user_info_version_value);
		versionTextView.setText("v"+mSession.getVersionName());
		mlzApplication.addActivity(this);
		daoSession = mlzApplication.getDaoSession();
		detectDao = daoSession.getDetectDao();
		contentDao = daoSession.getContentDao();
	}
	
	
	public void onSettingClick(View view){
		String uid = mSession.getUid();
		switch (view.getId()) {
		case R.id.id_spec_setting_list_back:
			this.finish();
			break;
		
		case R.id.id_user_info_help_label:
			startActivity(new Intent(this,SpecSettingHelpActivity.class));
			MobclickAgent.onEvent(mContext,"viewHelp");
			break;
		case R.id.id_user_info_introduce_label:
			startActivity(new Intent(this,SpecSettingIntroActivity.class));
			MobclickAgent.onEvent(mContext,"viewIntroduce");
			break;
		case R.id.id_user_info_clear_label:
			showCacheProcessDialog();
			MobclickAgent.onEvent(mContext,"clearCache");
			break;
		case R.id.id_user_info_version_label:
			new VersionManager(currrentActivity).showVersionProcessDialog(false);
			MobclickAgent.onEvent(mContext,"getNewVersion");
			break;
		case R.id.id_user_info_about_label:
			startActivity(new Intent(this,SpecSettingAboutActivity.class));
			MobclickAgent.onEvent(mContext,"viewAbout");
			break;
		case R.id.id_setting_feedback_label:
			startActivity(new Intent(this,SpecSettingListFeedBackActivity.class));
			MobclickAgent.onEvent(mContext,"viewFeedBack");
			break;
		case R.id.id_user_logout_label:
			
			if(uid != null && !"".equals(uid)){
				logout();
			}else{
				Toast.makeText(this,"亲，您还没有登录哦！",Toast.LENGTH_SHORT).show();
			}
			break;
		default:
			break;
		}
	}
	
	
	private Dialog logoutDialog = null;

	private void logout() {
			logoutDialog = new AlertDialog.Builder(mContext)
					.setTitle(R.string.customer_logout_title)
					.setMessage(R.string.customer_logout_msg)
					.setPositiveButton(R.string.customer_logout_certain,
							new OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									mlzApplication.removeActivities();
									MobclickAgent.onEvent(mContext,"logout");
									mSession.setUid("");
									startActivity(new Intent(SpecSettingListActivity.this,LoginActivity.class));
									logoutDialog.dismiss();
								}
							})
					.setNegativeButton(R.string.customer_logout_cancel,
							new OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									logoutDialog.dismiss();
								}
							}).create();
			logoutDialog.show();
	}
	/**
	 * 清除缓存过程的进度对话框
	 */
	private void showCacheProcessDialog(){
		cacheProgressDialog = ProgressDialog.show(this, "", "正在清理缓存……" ,true, false);
		contentDao.deleteAll();
		detectDao.deleteAll();
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				cacheProgressDialog.dismiss();
			}
		}, 3000);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("SplashScreen"); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
	    MobclickAgent.onResume(this);          //统计时长
	}

	@Override
	public void onPause() {
		super.onPause();
		 MobclickAgent.onPageEnd("SplashScreen"); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息 
		    MobclickAgent.onPause(this);
	}
}
